package cn.fuxi2022.accounts.reply.service.impl;

import cn.fuxi2022.accounts.reply.mapper.AccountsReplyMapper;
import cn.fuxi2022.accounts.reply.service.AccountsReplyService;
import cn.fuxi2022.core.utils.Query;
import cn.fuxi2022.model.empty.accounts.AccountsReply;
import cn.fuxi2022.model.resp.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 微信消息回复表 服务实现类
 * </p>
 *
 * @author Fuxi
 * @since 2022-10-18 08:27:33
 */
@Service
public class AccountsReplyServiceImpl extends ServiceImpl<AccountsReplyMapper, AccountsReply> implements AccountsReplyService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 1.构建搜索条件
        LambdaQueryWrapper<AccountsReply> wrapper = new LambdaQueryWrapper<>();
        // 按照Id排序
        wrapper.orderByAsc(AccountsReply::getId);

        // 2.获取请求参数，判断是否进行搜索查询
        // 搜索标题或内容
        if (ObjectUtils.isNotEmpty(params.get("search"))) {
            String search = String.valueOf(params.get("search"));
            wrapper.like(AccountsReply::getName,search).or().like(AccountsReply::getContent,search);
        }
        
        // 3.工具分页
        IPage<AccountsReply> page = this.page(
                new Query<AccountsReply>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public String getReplyContent(String keyword) {
        LambdaQueryWrapper<AccountsReply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountsReply::getName, keyword);
        AccountsReply reply;
        if (ObjectUtils.isNotEmpty(reply = this.getOne(wrapper))) {
            return reply.getContent();
        }
        return null;
    }
}
